package pl.donajski.magisterka;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class FileProtectionLoader extends FileLoader {

	public FileProtectionLoader(String fileName, List<Task> tasksContext) throws FileNotFoundException {
		super(fileName);
		this.mIndex = tasksContext.size();
		this.mTasksContext = tasksContext;
	}

	public List<Task> load() throws Exception {
		int count = Integer.parseInt(this.next());
		int isProtected = 0;
		List<Task> taskList = new ArrayList<Task>();
		
		for(int i = 0 ; i < count ; ++i) {
			isProtected = Integer.parseInt(this.next());
			if(isProtected == 1) {
				taskList.addAll(this.createTask(i));
			}
		}

		return taskList;
	}
	
	private List<Task> createTask(int projectIndex) {
		List<Task> bckupTasks = new ArrayList<Task>();
		
		for(Task t : mTasksContext) {
			if(t.getProjectIndex() != projectIndex || t.isPrimary() != true) 
				continue;
			Task bckup = new Task(++mIndex, 
					t.getInputTransmissionRate(),
					t.getOutputTransmissionRate(),
					t.getSourceNode(),
					true,
					t,
					t.getDestinations(),
					t.getProjectIndex());
			t.setBackupTask(bckup);
			bckupTasks.add(bckup);
		}
		return bckupTasks; 
	}

	private int mIndex = 0;
	private List<Task> mTasksContext;
}
